﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Text;
using WebUI.SmartDatingRef;

namespace WebUI {
    public class BasePage : System.Web.UI.Page {
        protected readonly SmartDatingClient Client = new SmartDatingClient();
        protected string SessionKey {
            get {
                return (string) Session["sessionKey"];
            }
            set {
                Session["sessionKey"] = value;
            }
        }
        protected string SessionKeyHash {
            get {
                return (string) Session["sessionKeyHash"];
            }
            set {
                Session["sessionKeyHash"] = value;
            }
        }

        public override void Dispose() {
            Client.Close();
            base.Dispose();
        }

        /// <summary>
        /// Converts base64-encoded string to bytes
        /// </summary>
        protected byte[] b(string s) {
            return Convert.FromBase64String(s);
        }

        /// <summary>
        /// Converts bytes to base64-encoded string
        /// </summary>
        protected string s(byte[] b) {
            return Convert.ToBase64String(b);
        }

        /// <summary>
        /// Converts UTF-16LE string to bytes
        /// </summary>
        protected byte[] bu(string s) {
            return Encoding.Unicode.GetBytes(s);
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (Request.Url.AbsolutePath.Equals(FormsAuthentication.LoginUrl,
                StringComparison.InvariantCultureIgnoreCase)) {
                return;
            }

            if (!Context.User.Identity.IsAuthenticated) {
                FormsAuthentication.RedirectToLoginPage();
                return;
            } else {
                if (SessionKey == null || SessionKeyHash == null) {
                    FormsAuthentication.SignOut();
                    FormsAuthentication.RedirectToLoginPage();
                    return;
                }
            }
        }
    }

    public partial class SiteMaster : System.Web.UI.MasterPage {
        protected readonly SmartDatingClient Client = new SmartDatingClient();
        protected string SessionKey {
            get {
                return (string)Session["sessionKey"];
            }
            set {
                Session["sessionKey"] = value;
            }
        }
        protected string SessionKeyHash {
            get {
                return (string)Session["sessionKeyHash"];
            }
            set {
                Session["sessionKeyHash"] = value;
            }
        }

        protected global::System.Web.UI.WebControls.LinkButton cmdLogout;

        protected void Page_Load(object sender, EventArgs e) {
        }

        protected void cmdLogout_Click(object sender, EventArgs e) {
            try {
                // logout from the forms
                FormsAuthentication.SignOut();

                // try to logout from the service
                if (SessionKeyHash != null) {
                    Client.Logout(SessionKeyHash);
                }
            } finally {
                FormsAuthentication.RedirectToLoginPage();
            }
        }

        protected void RedirectToReturn() {
            if (Request["ReturnUrl"] == null) {
                Response.Redirect("Default.aspx");
            } else {
                Response.Redirect((string)Request["ReturnUrl"]);
            }
        }

        public override void Dispose() {
            Client.Close();
            base.Dispose();
        }
    }
}
